package com.laizhenghua.hotel.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.laizhenghua.hotel.utils.PageUtils;
import com.laizhenghua.hotel.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.laizhenghua.hotel.entity.UsersEntity;
import com.laizhenghua.hotel.service.UsersService;
import org.springframework.web.servlet.ModelAndView;


/**
 * @author laizhenghua
 * @email 3299447929@qq.com
 * @date 2021-03-11 15:23:22
 */
@Controller
@RequestMapping("hotel/users")
public class UsersController {
    @Autowired
    private UsersService usersService;
    /**
     * 跳转创建用户页面
     */
    @GetMapping("users/add")
    public String toUsersAddPage(){
        return "admin/users/add";
    }
    /**
     * 处理普通用户申请会员请求
     */
    @GetMapping("/pass")
    public String updateUserGrade(String grade,String id,Model model){
        // System.out.println(grade);
        // System.out.println(id);
        Map<String,String> map = new HashMap<>();
        if("2".equals(grade)){
            map.put("type","2");
            map.put("grade",grade);
            map.put("id",id);
        }else{
            map.put("type","1");
            map.put("grade","1");
            map.put("id",id);
        }
        boolean isUpdate = usersService.updateUserGrade(map);
        if(isUpdate){
            UsersEntity usersEntity = usersService.getById(id);
            model.addAttribute("inpost",true);
            model.addAttribute("message","审核成功");
            model.addAttribute("state","success");
            model.addAttribute("user",usersEntity);
        }else {
            model.addAttribute("inpost",false);
            model.addAttribute("message","审核失败");
            model.addAttribute("state","error");
        }
        // return "admin/users/list";
        return "redirect:users/list";
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = usersService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ResponseBody
    public R info(@PathVariable("id") Integer id){
		UsersEntity users = usersService.getById(id);

        return R.ok().put("users", users);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ResponseBody
    public R save(@RequestBody UsersEntity users){
		usersService.save(users);

        return R.ok();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "/users/update",method = RequestMethod.POST)
    public R update(@RequestBody UsersEntity users){
		usersService.updateById(users);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids){
		usersService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 跳转至修改用户信息页面
     */
    @GetMapping("/profile/{user_id}")
    public String toProfilePage(@PathVariable("user_id") Integer user_id, Model model){
        UsersEntity usersEntity = usersService.getById(user_id);
        model.addAttribute("user",usersEntity);
        return "admin/users/profile";
    }
    /**
     * 跳转修改用户密码页面
     */
    @GetMapping("/password/{user_id}")
    public String toPasswordPage(@PathVariable("user_id") Integer user_id,Model model){
        UsersEntity usersEntity = usersService.getById(user_id);
        model.addAttribute("user",usersEntity);
        return "admin/users/password";
    }
    /**
     *
     */
    @GetMapping("/users/list")
    public ModelAndView toUserListPage(){
        List<UsersEntity> list = usersService.list();
        ModelAndView mav = new ModelAndView("admin/users/list");
        mav.addObject("list", list);
        return mav;
    }
}
